#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    loggedInForm_ = new LoggedInForm();
    loginForm_ = new LoginForm();

    ui->stackedWidget->insertWidget(0, loginForm_);
    ui->stackedWidget->insertWidget(1, loggedInForm_);

    ui->stackedWidget->setCurrentIndex(0);

    connect(loginForm_, SIGNAL(exitApplicationSignal()), this, SLOT(close()));
    connect(loginForm_, SIGNAL(loginSuccessful(QString)), this, SLOT(onLoginSuccess(QString)));
    connect(this, SIGNAL(loginSuccess(QString)), loggedInForm_, SLOT(onLoginSuccess(QString)));

    ui->action_Logout->setEnabled(false);

    connect(ui->action_Exit, SIGNAL(triggered()), this, SLOT(close()));
}

void MainWindow::onLoginSuccess(QString cookieValue) {
    emit loginSuccess(cookieValue);
    ui->menu_Logout->setEnabled(true);
    ui->stackedWidget->setCurrentIndex(1);
}

MainWindow::~MainWindow()
{
    delete ui;

    delete loginForm_;
}
